#ifndef TIMER_H
#define TIMER_H

#include <chrono>
#include <functional>

namespace Timer
{
    class Timer
    {
    public:
        using Timestamp = std::chrono::system_clock::time_point;
        using TimeInterval = std::chrono::duration<double>;
        using TimerCallback = std::function<void()>;
        Timer(Timestamp timeout, TimeInterval interval, const TimerCallback &cb) // 小对象无需引用传递
            : timeout_(timeout),
              interval_(interval),
              callback_(std::move(cb))
        {
        }

        Timestamp getTimeout() const
        {
            return this->timeout_;
        }
        TimeInterval getInterval() const
        {
            return this->interval_;
        }

        void exec()
        {
            this->callback_();
        }
        void restart(Timestamp now);

    private:
        Timestamp timeout_;      // 超时时间
        TimeInterval interval_;  // 触发间隔（0为不重复）
        TimerCallback callback_; // 回调函数
    };
} // namespace Timer

#endif // TIMER_H